package com.mate.cloud.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mate.cloud.core.util.ObjectUtil;
import com.mate.cloud.constant.LikedStatusEnum;
import com.mate.cloud.dataobject.UserInfo;
import com.mate.cloud.dataobject.UserLike;
import com.mate.cloud.dto.LikedCountDTO;
import com.mate.cloud.mapper.UserLikeMapper;
import com.mate.cloud.service.LikedService;
import com.mate.cloud.service.RedisService;
import com.mate.cloud.service.UserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class LikedServiceImpl extends ServiceImpl<UserLikeMapper, UserLike> implements LikedService {

    @Resource
    UserLikeMapper userLikeMapper;

    @Resource
    RedisService redisService;

    @Resource
    UserService userService;


    @Override
    @Transactional
    public boolean save(UserLike like) {
        return userLikeMapper.insert(like) > 0;
    }



    @Override
    public Page<UserLike> getLikedListByLikedUserId(String likedUserId, Pageable pageable) {
        return userLikeMapper.findByLikedUserIdAndStatus(likedUserId, LikedStatusEnum.LIKE.ordinal(), pageable);
    }

    @Override
    public Page<UserLike> getLikedListByLikedPostId(String likedPostId, Pageable pageable) {
        return userLikeMapper.findByLikedPostIdAndStatus(likedPostId, LikedStatusEnum.LIKE.ordinal(), pageable);
    }

    @Override
    public UserLike getByLikedUserIdAndLikedPostId(String likedUserId, String likedPostId) {
        return userLikeMapper.findByLikedUserIdAndLikedPostId(likedUserId, likedPostId);
    }

    @Override
    @Transactional
    public void transLikedFromRedis2DB() {
        List<UserLike> likedCountFromRedis = redisService.getLikedDataFromRedis();

        likedCountFromRedis.forEach(like -> {
            UserLike user = getByLikedUserIdAndLikedPostId(like.getLikedUserId()
                    , like.getLikedPostId());
            if (ObjectUtil.isEmpty(user)) {
                //没有记录，直接存入
                save(like);
            } else {
                //有记录，需要更新
                user.setStatus(like.getStatus());
                save(user);
            }
        });
    }

    @Override
    @Transactional
    public void transLikedCountFromRedis2DB() {
        List<LikedCountDTO> likedCountFromRedis = redisService.getLikedCountFromRedis();
        likedCountFromRedis.forEach(likedCountDTO -> {
            UserInfo userInfo = userService.findById(likedCountDTO.getId());
            // 点赞数量属于无关紧要的操作，出错无需抛异常
            if (ObjectUtil.isNotEmpty(userInfo)) {
                int likeNum = userInfo.getLikeNum() + likedCountDTO.getCount();
                userInfo.setLikeNum(likeNum);
                // 更新点赞数量
                userService.updateInfo(userInfo);
            }
        });
    }
}
